
package CCT220_Trabalho04_13106.view;


import java.util.LinkedList;
import java.util.Vector;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;

import CCT220_Trabalho04_13106.control.ControleTurma;
import CCT220_Trabalho04_13106.model.Disciplina;
import CCT220_Trabalho04_13106.model.Estudante;
import CCT220_Trabalho04_13106.model.Turma;


/**
 * </br>******************************
 * </br>Project: CCT220
 * </br>Package: CCT220_Trabalho04_13106.view
 * </br>Class: LimiteTurma
 * </br>Last Change: 08/10/2011 11:23:39 by marcel
 * </br>Description: Class the manage the limit between the Turma entity and the
 * user
 * </br>******************************</br>
 * 
 * @author marcel
 * @author 13106
 * 
 */
public class LimiteTurma {

	/**
	 * ControleTurma
	 */
	private final ControleTurma ctrTurma;


	/**
	 * </br>******************************
	 * </br>Constructor for
	 * </br>Package: CCT220_Trabalho04_13106.view
	 * </br>Class: LimiteTurma
	 * </br>LastChange: 08/10/2011 11:23:41 by marcel
	 * </br>******************************</br>
	 * 
	 * @param ctrTurma
	 */
	public LimiteTurma(ControleTurma ctrTurma) {

		this.ctrTurma = ctrTurma;
	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.view
	 * </br>Class: LimiteTurma
	 * </br>Method: montaMenu
	 * </br>Last Change: 08/10/2011 11:23:43 by marcel
	 * </br>Description: Method that show the options to the user and return the
	 * chosen one
	 * </br>******************************</br>
	 * 
	 * @return the chosen option
	 */
	public int montaMenu() {

		int escolha = -1;
		String escolhaInformada = "";
		do {

			escolhaInformada = JOptionPane
			        .showInputDialog("Escolha uma opção do menu principal>turma:\n"
			                + "[1] Adicionar turma\n"
			                + "[2] Listar uma turma\n"
			                + "[3] Listar todas turmas\n"
			                + "[4] Remover turma\n" + "[5] Voltar & salvar ");
			try {
				escolha = Integer.parseInt(escolhaInformada);
				if ((escolha < 1) || (escolha > 5)) {
					JOptionPane.showMessageDialog(null,
					        "Use apenas números entre 1 e 5!!!", "Erro",
					        JOptionPane.ERROR_MESSAGE);
				}
			} catch (NumberFormatException nExc) {
				JOptionPane.showMessageDialog(null, "Use apenas números!!!",
				        "Erro", JOptionPane.ERROR_MESSAGE);
			}

		} while ((escolha < 1) || (escolha > 5));
		return escolha;
	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.view
	 * </br>Class: LimiteTurma
	 * </br>Method: capturaDadosCadastrais
	 * </br>Last Change: 08/10/2011 11:23:46 by marcel
	 * </br>Description: Method that capture the data for the new entity of
	 * Turma
	 * </br>******************************</br>
	 * 
	 * @param linkedListEstudantes
	 * @param linkedListDisciplinas
	 * @return the new Turma
	 */
	public Turma capturaDadosCadastrais(
	        LinkedList<String> linkedListEstudantes,
	        LinkedList<String> linkedListDisciplinas) {

		String dados[] = new String[2];
		dados[0] = JOptionPane.showInputDialog("Digite o codigo para a turma");
		dados[1] = JOptionPane.showInputDialog("Digite o nome da turma");
		Disciplina d = this.selectDisciplina(linkedListDisciplinas);
		Vector<Estudante> listaEstudantes = this
		        .selectEstudantes(linkedListEstudantes);

		return new Turma(Integer.parseInt(dados[0]), dados[1], d,
		        listaEstudantes);
	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.view
	 * </br>Class: LimiteTurma
	 * </br>Method: selectDisciplina
	 * </br>Last Change: 08/10/2011 11:23:48 by marcel
	 * </br>Description: Method that select a Disciplina from the given list
	 * </br>******************************</br>
	 * 
	 * @param linkedListDisciplinas
	 * @return Disciplina
	 */
	private Disciplina selectDisciplina(LinkedList<String> linkedListDisciplinas) {

		JTextArea jta = new JTextArea();
		jta.setBackground(new JLabel().getBackground());
		jta.setForeground(new JLabel().getForeground());
		jta.setEditable(false);
		jta.setText("Digite o codigo de uma das disciplinas:\n\n"
		        + linkedListDisciplinas);
		int choice = -1;
		do {
			choice = Integer.parseInt(JOptionPane.showInputDialog(null, jta,
			        "Escolha da Disciplina da Turma",
			        JOptionPane.QUESTION_MESSAGE));
			if (!ctrTurma.isInListDisciplina(choice)) {
				JOptionPane.showMessageDialog(null, "Disciplina de codigo "
				        + choice + " nao encontrada na lista de disciplinas",
				        "Erro", JOptionPane.ERROR_MESSAGE);
			}
		} while (!ctrTurma.isInListDisciplina(choice));
		return ctrTurma.getInListDisciplina(choice);

	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.view
	 * </br>Class: LimiteTurma
	 * </br>Method: selectEstudantes
	 * </br>Last Change: 08/10/2011 11:23:51 by marcel
	 * </br>Description: Method that select some Estudante from the given list
	 * </br>******************************</br>
	 * 
	 * @param linkedListEstudantes
	 * @return Vector<Estudante>
	 */
	private Vector<Estudante> selectEstudantes(
	        LinkedList<String> linkedListEstudantes) {

		JTextArea jta = new JTextArea();
		jta.setBackground(new JLabel().getBackground());
		jta.setForeground(new JLabel().getForeground());
		jta.setEditable(false);
		jta.setText("Digite o codigo de um estudante ou 's' para sair:\n\n"
		        + linkedListEstudantes);
		Vector<Estudante> listaEstudantes = new Vector<Estudante>();
		int choice = -1;
		do {
			String s = JOptionPane.showInputDialog(null, jta,
			        "Escolha da Disciplina da Turma",
			        JOptionPane.QUESTION_MESSAGE);
			if (s.equalsIgnoreCase("s")) {
				break;
			} else {
				choice = Integer.parseInt(s);
				if (!ctrTurma.isInListEstudante(choice)) {
					JOptionPane.showMessageDialog(null,
					        "Estudante de codigo " + choice
					                + " nao encontrado na lista de estudantes",
					        "Erro", JOptionPane.ERROR_MESSAGE);
				} else {
					listaEstudantes.add(ctrTurma.getInListEstudante(choice));
				}
			}

		} while (true);
		return listaEstudantes;
	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.view
	 * </br>Class: LimiteTurma
	 * </br>Method: selectOne
	 * </br>Last Change: 08/10/2011 11:23:54 by marcel
	 * </br>Description: Method that select one Turma from the given list and
	 * return its code
	 * </br>******************************</br>
	 * 
	 * @param list
	 * @return code of the selected Turma
	 */
	public int selectOne(LinkedList<String> list) {

		JTextArea jta = new JTextArea();
		jta.setBackground(new JLabel().getBackground());
		jta.setForeground(new JLabel().getForeground());
		jta.setEditable(false);
		jta.setText("Digite o codigo de um dos itens abaixo:\n\n" + list);
		int code = -1;
		code = Integer.parseInt(JOptionPane.showInputDialog(null, jta,
		        "Selecao de Turma", JOptionPane.INFORMATION_MESSAGE));
		return code;
	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.view
	 * </br>Class: LimiteTurma
	 * </br>Method: show
	 * </br>Last Change: 08/10/2011 11:23:56 by marcel
	 * </br>Description: Method that show the given String in a JOptionPane
	 * </br>******************************</br>
	 * 
	 * @param string
	 */
	public void show(String string) {

		JTextArea jta = new JTextArea();
		jta.setBackground(new JLabel().getBackground());
		jta.setForeground(new JLabel().getForeground());
		jta.setEditable(false);
		jta.setText(string);
		JOptionPane.showMessageDialog(null, jta, "Turma",
		        JOptionPane.INFORMATION_MESSAGE);

	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.view
	 * </br>Class: LimiteTurma
	 * </br>Method: showAll
	 * </br>Last Change: 08/10/2011 11:23:58 by marcel
	 * </br>Description: Method that show the given LinkedList<String> in a
	 * JOptionPane
	 * </br>******************************</br>
	 * 
	 * @param list
	 */
	public void showAll(LinkedList<String> list) {

		JTextArea jta = new JTextArea();
		jta.setBackground(new JLabel().getBackground());
		jta.setForeground(new JLabel().getForeground());
		jta.setEditable(false);
		jta.setText("Lista de Turmas: \n\n" + list);
		JOptionPane.showMessageDialog(null, jta, "Lista Turmas",
		        JOptionPane.INFORMATION_MESSAGE);

	}

}
